/**
 * Copyright (c) 2011 Technical Centre Radio Systems Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ua.tcrs.tabula.startup;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

import ua.tcrs.tabula.log.Logger;
import ua.tcrs.tabula.log.NopLogger;
import ua.tcrs.tabula.util.Iterables;
import ua.tcrs.tabula.container.ServiceFactory;
import ua.tcrs.tabula.container.ServiceRegistry;
import ua.tcrs.tabula.container.Container;
import ua.tcrs.tabula.container.BaseContainer;
import ua.tcrs.tabula.container.BaseServiceRegistry;
import ua.tcrs.tabula.container.AopServiceRegistry;

public class ContainerBuilder {
	public ContainerBuilder setLogger( Logger logger ) {
		if ( logger != null ) this.logger = logger;
		return this;
	}
	public ContainerBuilder useAop() {
		useAop = true;
		return this;
	}
	public ContainerBuilder addModule( Object module ) {
		modules.add( module );
		return this;
	}
	public ContainerBuilder addModules( Iterable<Object> modules ) {
		this.modules.addAll( Iterables.toCollection( modules ) );
		return this;
	}
	public ContainerBuilder addModules( Object... modules ) {
		this.modules.addAll( Arrays.asList( modules ) );
		return this;
	}
	public ContainerBuilder bind( Object service ) {
		services.add( service );
		return this;
	}
	public ContainerBuilder setParentServiceFactory( ServiceFactory factory ) {
		parentServiceFactory = factory;
		return this;
	}
	public Container build() {
		ServiceRegistry registry = useAop ? new AopServiceRegistry( parentServiceFactory ) : new BaseServiceRegistry( parentServiceFactory );
		for ( Object service: services ) registry.bindService( service );
		return new BaseContainer( modules, logger, registry );
	}
	protected List<Object> modules = new ArrayList<Object>();
	protected List<Object> services = new ArrayList<Object>();
	protected Logger logger = new NopLogger();
	protected ServiceFactory parentServiceFactory = null;
	protected boolean useAop = false;
}
